set(parser_odir "${CMAKE_CURRENT_BINARY_DIR}/")
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${parser_odir})

#The right way to me would be to use the commands (2) and (4)
#but it seems one needs (1) and (3) instead, if regeneration
#of the language_scanner.re and parser.y is desired with only a "make"
#command.
#
#Why I think that would be the right way? Because commands
#have output (in cmake terms) and targets don't.

add_custom_target(language_parser
	COMMAND ${CMAKE_COMMAND} -E copy "language_parser.y" "${parser_odir}language_parser.y"
#(1):
	COMMAND ${PROJECT_BINARY_DIR}/lemon/lemon d="${CMAKE_CURRENT_SOURCE_DIR}/lempar.c" "${parser_odir}language_parser.y"
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	COMMENT "add_custom_target(language_parser)"
	)
add_custom_command(OUTPUT "${parser_odir}language_parser.c"
#(2):
#	COMMAND ${PROJECT_BINARY_DIR}/lemon/lemon d="${CMAKE_CURRENT_SOURCE_DIR}/lempar.c" "${parser_odir}language_parser.y"
	COMMENT "add_custom_command(parser)"
	)

add_custom_target(re2c_scanner
#(3):
	COMMAND re2c "${CMAKE_CURRENT_SOURCE_DIR}/language_scanner.re" > "${parser_odir}language_scanner.c"
	COMMENT "add_custom_target(re2c_scanner)"
	)
add_custom_command(OUTPUT "${parser_odir}language_scanner.c"
#(4):
#	COMMAND re2c -i "${CMAKE_CURRENT_SOURCE_DIR}/language_scanner.re" > "${parser_odir}language_scanner.c"
	COMMENT "add_custom_command(scanner)"
	)
add_library(parser STATIC ypl_parser.c ast.c "${parser_odir}language_scanner.c" "${parser_odir}language_parser.c")
add_dependencies(parser language_parser re2c_scanner)
add_dependencies(language_parser lemon)
